<?php
namespace app\home\controller;
use think\Controller;

class Food extends Controller
{
    //特价菜品列表（按时间降序排列）
    public function bargainpricelists()
    {
      

        //循环遍历数据库的food,查看food是否在特价时间段，如果是，isspecial设置为1，不是为0
        $foodList=\think\Db::name('food')->select();
        for ($i=0; $i <count($foodList)-1 ; $i++) { 
           $id=$foodList[$i]['food_id'];
           $endDate=$foodList[$i]['enddate'];
           $startDate=$foodList[$i]['startdate'];
           $status=$foodList[$i]['isspecial'];
           $now=time();
           if ($now>=strtotime($startDate)&&$now<=strtotime($endDate)) {
               $status=1;
           }
           else{
            $status=0;
           }
           
           $data['isspecial']=$status;
           \think\Db::name('food')->where("food_id='$id'")->update($data);
        }

        //再次取出数据,并分页
        $newFoodList=\think\Db::view('food','foodname,pic,price,shop_id,isspecial,food_id')
        ->view('dining','fei,obserave,shop_id,level','food.shop_id=dining.shop_id')
        ->where('food.isspecial=1')
        ->paginate(6);
        //分页数据
        $page=$newFoodList->render();


        //销量最高（特价菜品按订单数降序排列）
        $topFoodList=\think\Db::name('food')
        ->alias('f')
        ->join('order','f.food_id=order.food_id')
        ->field('f.shop_id,f.foodname,sum(order.num),order.food_id')
        ->group('order.food_id')
        ->order('sum(order.num) desc')
        ->where('f.isspecial=1')
        ->limit(5)
        ->select();
        //0元起送（特价菜品所属店铺的配送费为0按时间降序排列）

        $this->assign(array('page'=>$page,'newFoodList'=>$newFoodList,'topFoodList'=>$topFoodList));
        return $this->fetch();
    }

    
    //热门菜品列表（按订单数降序排列）
     public function hotfoodlists()
    {
        //口碑菜品（按订单数降序排列）
        //1.页面设计的美食评分无 ，暂时为店铺评分字段替代
        //2.无美食浏览量字段 ，暂时为店铺浏览字段替代
        $hotFoodList=\think\Db::name('order')
        ->alias('o')
        ->join('dining','o.shop_id=dining.shop_id')
        ->join('food','o.food_id=food.food_id')
        ->field('dining.shop_id,food.foodname,sum(o.num),o.food_id,food.pic,dining.fei,dining.obserave,food.price')
        ->group('o.food_id')
        ->order('sum(o.num) desc')
        ->paginate(6);
         $page=$hotFoodList->render();

        //低价配送（按订单数降序且配送费升序排列）

        
         $lowList=\think\Db::name('order')
        ->alias('o')
        ->join('dining','o.shop_id=dining.shop_id')
        ->join('food','o.food_id=food.food_id')
        ->field('dining.shop_id,food.foodname,dining.fei')
        ->group('o.food_id')
        ->order('sum(o.num) desc')
        ->where('dining.fei=0')
        ->limit(5)
        ->select();


        //新品进店（按订单数及时间顺序降排列）(需求模糊，暂时不写)
        $this->assign(array('hotFoodList'=>$hotFoodList,'page'=>$page,'lowList'=>$lowList));
        return $this->fetch();
    }


    //新进菜品列表（按时间降序排列）
     public function newfoodlists()
    {
        //新品尝鲜（按时间降序排列）
        //$newFoodList=\think\Db::name('food')->order('createtime desc')->paginate(6);
        $newFoodList=\think\Db::view('food','foodname,pic,shop_id,addate')
        ->view('dining','fei,level,obserave,shop_id','food.shop_id=dining.shop_id')
        ->order('food.addate desc')
        ->paginate(6);
        $page=$newFoodList->render();

        //销量最高（按订单数以及时间降序排列）
        $topFoodList=\think\Db::name('food')
        ->alias('f')
        ->join('order','f.food_id=order.food_id')
        ->field('f.shop_id,f.foodname,sum(order.num),order.food_id')
        ->group('order.food_id')
        ->order('sum(order.num) desc')
        ->limit(5)
        ->select();
         //不可错失（按时间及浏览量降序排列）(需求不明确)
        $this->assign(array('newFoodList'=>$newFoodList,'page'=>$page,'topFoodList'=>$topFoodList));
        return $this->fetch();
    }

}
